<?php
/**
 * 【司机信息控制器】
 * Created by PhpStorm.
 * User: 孙凯
 * Date: 2018-06-05
 * Time: 13:48
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Support\Facades\DB;

class ChaufferurInfoController extends Controller
{
    /**
     * 【司机信息列表】
     * @Author sk
     * @DateTime 2018-06-05
     * @param Request $request
     * @return array
     */
    public function info(Request $request)
    {
        $starttime = strtotime($request['starttime']);
        $endtime = strtotime($request['endtime']);
        if (isset($request['starttime']) || isset($request['endtime'])) {
            $handle = DB::table('chauffeur_info_apply')
                ->join('approval_log', 'chauffeur_info_apply.number', '=', 'approval_log.number')//差司机
                ->join('driver', 'chauffeur_info_apply.driver_id', '=', 'driver.id')//差司机
                ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
                ->join('branch', 'convoy.branch_id', '=', 'branch.id');//机构;
        } else {
            $handle = DB::table('chauffeur_info_apply')
                ->join('approval_log', 'chauffeur_info_apply.number', '=', 'approval_log.number')//差司机
                ->join('driver', 'chauffeur_info_apply.driver_id', '=', 'driver.id')//差司机
                ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
                ->join('branch', 'convoy.branch_id', '=', 'branch.id');//机构;
        }
        $datas = $handle->get();
        // dd($datas);
        $data = json_decode(json_encode($datas), true);
        return response(['mag' => $data, 'code' => 1000, 'message' => '数据返回成功！'], 200);

    }

    /**
     * 【司机信息审批详情展示】
     * @type   int 字段 1：同意请假，2拒绝请假，3：打回
     * @id  int 司机id
     * @Author sk
     * @DateTime 2018-06-05
     * @param Request $request
     * @return array
     */
    public function info_show(Request $request)
    {
        $array = $request->all();
        try {
            if (empty($array['driver_id']) || empty($array['number'])) {
                return '司机id或申请单号为空，请填写';
            }
            $id = $array['driver_id'];
            //查询司机的信息
            $info = Db::table('driver')->where('id', $id)->first();
            $info = get_object_vars($info);
            //查询车队的信息
            //查询的字段
            $convoydata = [
                'name',
                'branch_id',
                'captain_id',
            ];
            $convoy = Db::table('convoy')->where('id', $info['convoy_id'])->select($convoydata)->first();
            $convoy = get_object_vars($convoy);
            //查询队长姓名
            $captainname = Db::table('driver')->where('id', $convoy['captain_id'])->select('driver_name')->first();
            $captainname = get_object_vars($captainname);
            //查询机构的信息
            $branch = Db::table('branch')->where('id', $convoy['branch_id'])->select('company_name')->first();
            $branch = get_object_vars($branch);
            $info['name'] = $convoy['name'];
            $info['driver_name'] = $captainname['driver_name'];
            $info['company_name'] = $branch['company_name'];
            //审核日志查询
            $number = $array['number'];
            $appol = Db::table('approval_log')->where('number', $number)->first();
            $appol = json_decode(json_encode(array($appol)), true);
            //$appol['time'] = date('Y-m-d H:i:s', $appol['time']);
           // $v['time']
            //dd($appol);
            foreach ($appol as $k => $v) {
                $info['role_id'] = $v['role_id'];
                $info['lead'] = $v['lead'];
                $tt = date('Y-m-d H:i:s', $v['time']);
                $dd = $v['lead'];
                $appol[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
            }
           // dd($appol);
          //  $info = get_object_vars($info);

            foreach ($appol as $k => &$val) {
                // $info= get_object_vars($info);
                switch ($val['results']) {
                    case 0:
                        $pass[$k]['title'] = '创建';
                        $pass[$k]['name'] = $info['driver_name'];
                        $pass[$k]['time'] = $tt;
                        break;
                    case 1:
                        $pass[$k]['title'] = '拒绝';
                        $pass[$k]['name'] = $dd;
                        $pass[$k]['time'] = $tt;
                        break;
                    case 2:
                        $pass[$k]['title'] = '打回';
                        $pass[$k]['name'] = $dd;
                        $pass[$k]['time'] = $tt;
                        break;
                    case 3:
                        $pass[$k]['title'] = '完成';
                        $pass[$k]['time'] = $tt;
                        break;
                }
            }
            /**
             * t头部循环
             */
            $data = DB::table('chauffeur_info_apply')->where('number', $number)->first();
            $data = json_decode(json_encode($data), true);
            $data = json_decode(json_encode(array($data)));
            // dd($data);
            $info = json_decode(json_encode($info), true);


            return response(['appol' => $appol, 'info' => $info, 'data' => $pass, 'code' => 1000, 'message' => '信息返回成功！'], 200);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }
    }

    /**
     * 【司机信息详情审批】
     * @type  account_status 字段 1：同意请假，2拒绝请假，3：打回
     * @id 司机id
     * @Author sk
     * @DateTime 2018-06-05
     * @param Request $request
     * @return array
     */
    public function info_detail(Request $request)
    {
        $data = $request->all();
        $number = Db:: table('approval_log')->where('number', $data['number'])->value('results');
        //dd($number);
        if ($number == '1') {
            return response(['msg' => '', 'code' => 1000, 'message' => '司机信息已经审批成功！'], 200);
        }
        try {
            $id = $data['driver_id'];
            if ($data['type'] == 1) {
                #type==1 为同通过司机申请
                $leave = Db:: table('driver')->where('id', $id)->update(['account_status' => 1]);
                $sg = Db:: table('approval_log')->where('number', $data['number'])
                    ->update([
                        'role_id' => $data['role_id'],
                        'lead' => $data['lead'],
                        'time' => time(),
                        'results' => $data['type'],
                        'opinion' => $data['opinion'],
                        'circuit_number' => date('his') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
                        'number' => date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 10), 1))), 0, 6),
                    ]);
                $msg = json_decode(json_encode($sg), true);
                return $msg ? response(['msg' => $msg, 'code' => 1000, 'message' => '司机信息审批通过成功！'], 200) : response(['msg' => '', 'code' => 1011, 'message' => '司机信息审批通过失败！'], 400);

            } elseif ($data['type'] == 2) {
                //type==2 为拒绝
                $status = Db::table('driver')->where('id', $id)->update('account_status', 1);
                $sg = Db:: table('approval_log')->where('number', $data['number'])
                    ->update([
                        'role_id' => $data['role_id'],
                        'lead' => $data['lead'],
                        'time' => time(),
                        'results' => $data['type'],
                        'opinion' => $data['opinion'],
                        'circuit_number' => date('his') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
                        'number' => date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 10), 1))), 0, 6),
                    ]);
                $dataform = json_decode(json_encode($sg && $status), true);

                if ($dataform) {
                    return response(['msg' => $dataform, 'code' => 1000, 'message' => '司机信息审批拒绝成功！'], 200);
                } else {
                    return response(['msg' => '', 'code' => 1011, 'message' => '司机信息审批拒绝失败！'], 400);
                }
            } elseif ($data['type'] == 3) {
                #type==3 为的打回司机申请
                $status3 = Db::table('driver')->where('id', $id)->update('account_status', 5);
                Db:: table('approval_log')->where('number', $data['number'])
                    ->update([
                        'role_id' => $data['role_id'],
                        'lead' => $data['lead'],
                        'time' => time(),
                        'results' => $data['type'],
                        'opinion' => $data['opinion'],
                        'circuit_number' => date('his') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
                        'number' => date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 10), 1))), 0, 6),
                    ]);
                $msgg = json_encode($status3);
                return $msgg ? response(['msg' => '', 'code' => 1001, 'message' => 'id为' . $id . '的司机打回成功成功！'], 200) : response(['msg' => '', 'code' => 1011, 'message' => '司机打回失败！'], 400);
            } else {
                return response(['msg' => '', 'code' => 1011, 'message' => '传递参数错误！'], 400);
            }
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }
    }


}